package com.ihai.spring.boot.sample.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.x.discovery.ServiceInstance;

import java.util.List;

/**
 * DESC
 *
 * @author 张驰
 * @date 2019/6/4
 */
public class ZkServiceConsumer {
    public static void main(String[] args) throws Exception {
        String host = "192.168.16.222";
        String port = "2181";
        ZkService service = new ZkService();
        service.start(host, port, "/esb/test");
        List<ServiceInstance<String>> serviceInstanceList = service.getInstance("zkServiceProvider");
        if (serviceInstanceList != null) {
            for (ServiceInstance<String> serviceInstance : serviceInstanceList) {
                System.out.println(serviceInstance.getPayload());
            }
        }
        service.watch("zkServiceProvider", new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
                if (event.getData() != null) {
                    System.out.println(event.getData().getPath());
                }
            }
        });
        Thread.sleep(10000000);
        service.close();
    }
}
